/**
 * FileName: WechatIndexController
 * Author:   Leopold
 * Date:     2018/9/4 11:16
 * Description: 微信入口
 * History:
 */
package cn.mcsys.platform.core.web;

import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirRealtimeMessage;
import cn.mcsys.platform.core.service.PlatformService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AirRidePointsController {

    private static Logger logger = LoggerFactory.getLogger(AirRidePointsController.class);

    @Autowired
    private BackService backService;

    @Autowired
    private PlatformService platformService;


    @RequestMapping("airRidePoints")
    public String airRidePoints(Model model) {
        System.out.println("go to airRidePoints!getting data");
        Map<String, Object> result = platformService.getRidePointDtoListMap();
        model.addAttribute("dataMap", result);
        return "airRidePoints";
    }

    /**
     * 乘车点概况chart1 专线比例饼图
     *
     * @return
     */
    @GetMapping("getAllCar")
    @ResponseBody
    public JsonResult getAllCar() {
        return JsonResult.success(backService.getAllCarList());
    }

    /**
     * 乘车点概况chart2 专线柱状图
     *
     * @param airRidePointId
     * @return
     */
    @GetMapping("getRidePointBarData")
    @ResponseBody
    public JsonResult getAirShowAreaData(Integer airRidePointId) {
        Map<String, Object> ridePointBarData = platformService.getRidePointBarData(airRidePointId);
        return JsonResult.success(ridePointBarData);
    }


    /**
     * 乘车点table 已投入|待发车|已发车|已到达|已返程
     *
     * @param airRidePointId
     * @return
     */
    @GetMapping("getCarByPointId")
    @ResponseBody
    public JsonResult getCarByPointId(Integer airRidePointId) {
        return JsonResult.success(backService.selectByPointId(airRidePointId));
    }

    /**
     * 乘车点table 候车人数|预约人数|已运送人数
     *
     * @param airRidePointId
     * @return
     */
    @GetMapping("getTableData")
    @ResponseBody
    public JsonResult getTableData(Integer airRidePointId) {
        return JsonResult.success(platformService.getTableData(airRidePointId));
    }

    /**
     * 乘车点table 即将返程|平均耗时
     *
     * @param airRidePointId
     * @return
     */
    @GetMapping("getAvgConsume")
    @ResponseBody
    public JsonResult getAvgConsume(Integer airRidePointId) {
        return JsonResult.success(platformService.getAvgConsume(airRidePointId));
    }

    /**
     * 乘车点table 前一趟耗时
     *
     * @param airRidePointId
     * @return
     */
    @GetMapping("getLeastConsume")
    @ResponseBody
    public JsonResult getLeastConsume(Integer airRidePointId) {
        return JsonResult.success(platformService.getLeastConsume(airRidePointId));
    }

    @GetMapping("getLeastRecord")
    @ResponseBody
    public JsonResult getLeastRecord(Integer airRidePointId) {
        List<AirRealtimeMessage> airRealtimeMessages = backService.getMessageLeastByPointId(airRidePointId);
        if(airRealtimeMessages.size()>0){
            for (AirRealtimeMessage message : airRealtimeMessages) {
                if(message.getNote().indexOf("候车")>0){
                    return JsonResult.success(message);
                }
            }
        }
        return JsonResult.fail();
    }

}
